<?php

/**
 * @filesource Contacts the Student control file to manipulate Student data in
 * the student table.
 *
 * @copyright 2011 by Dan, Steven, Brice, Mike, and Aaron.  This program is part
 * of Internship 2 Career, which is free software. It comes with absolutely no
 * warranty. You can redistribute or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation (For
 * more information, see <http://www.gnu.org/licenses/).
 *
 * @version February 10, 2011
 *
 * @author Steven Simard
 *
 */
echo "<html><body>";
include '../db-control/inc/functions.php';
include '../model/student.php';
include '../db-control/dbstudent.php';

/**
 * update a student if the user is trying to do so
 */
if ($_POST['update'] != '') {
    $id = $_POST['update'];
    $s = new student(); //creates a new instance of student()
    $s->setID($id); //sets the student ID
    $db = new dbStudent();
    $exists = $db->checkID($s); //checks to see if the ID is a real student
    if (!$exists) {
        echo "<b>student with ID '$id' does not exist...</b>";
    } else {
        $s->setFName($_POST['fname']); //sets the student first name
        $s->setLName($_POST['lname']); //sets the student last name
        $result = $db->updateStudent($s); //finally, updates the student with
        //the data we've set
        if (!$result) {
            echo "<b>Could not update user with ID '$id'...</b>";
        }
    }
} else {
    if (isset($_POST['add'])) {
        $s = new Student();
        $s->setFName($_POST['fname']);
        $s->setLName($_POST['lname']);
        $db = new dbstudent();
        $result = $db->addstudent($s);
        if (!$result) {
            echo "<b>Please enter some student information</b>";
        }
    }

    if (isset($_POST['deleteButton'])) {
        $s = new student(); //creates a new student object 
        $s->setID($_POST['deleteID']);
        $db = new dbstudent();
        $result = $db->deletestudent($s);
        if (!$result) {
           echo "<b>Could not delete student with ID '$_POST[deleteID]'...</b>";
        }
    }
}


//displays the web form used for adding a new student
echo <<<_FORM


<form action='viewstudent.php' method='post'>
<table>
    <tr><td colspan='2'>Specify an ID to change a student's data</td><td>
    <input type='text' size='4' name='update' /></td></tr>
    <tr><td>First Name:</td><td><input type='text' value='' name='fname' />
    </td></tr>
    <tr><td>Last Name:</td><td><input type='text' value='' name='lname' />
    </td></tr>
    <tr><td></td><td align='right'><input type='submit' value='Add student'
    name='add'/></td></tr>
</table>
</form>


_FORM;

displaystudents(); //displays the student list


echo "</body></html>";

function displaystudents() {
    $db = new dbStudent(); //gets all the students from the database
    $result = $db->getAllStudents();
    echo "<table width='200px'>";
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        echo "<tr><td>$row[id]</td><td></td></tr>";
        echo "<tr><td align='right'>First Name:</td><td align='left'>$row[fname]
        </td></tr>";
        echo "<tr><td align='right'>Last Name:</td><td align='left'>$row[lname]
        </td></tr>";
        echo "<tr><td colspan='2' align='right'><form action='viewstudent.php'
        method='post'><input type='hidden' value='$row[id]' name='deleteID' />
        <input type='submit' value='Delete' name='deleteButton' /></form></td>
        <td></td></tr>";
        echo "<tr><td colspan='2'><hr /></td><td></td></tr>";
    }
    echo "</table>";
}
?>


